using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using ToDoListWeb.Models;
using ToDoListWeb.ViewModels;

namespace ToDoListWeb.Service
{
    public class InventoryService
    {
        private ToDoListWebContext _ctx = new ToDoListWebContext();
        TypeService _typeService = new TypeService();

        /// <summary>
        /// 创建清单；
        /// 根据所给数据新建一个Inventory对象；
        /// 将该对象添加到user下的Inventory列表和_ctx下的Inventory列表中；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="inventoryName"></param>
        public void CreateInventory(int typeId, string inventoryName)                    //创建清单
        {
            TypeService Type = new TypeService();
            var type = Type.GetType(typeId);
            Inventory inventory = new Inventory()
            {
                UserId = type.UserId,
                InventoryName = inventoryName,
                TypeId=type.TypeId
                
            };
            _ctx.Inventory.Add(inventory);
            _ctx.SaveChanges();
        }
        /// <summary>
        /// 删除清单；
        /// 若在该user中的inventory列表中无该inventoryId匹配项，抛出异常；
        /// 若匹配到该Inventory对象，则删除该清单以及该清单下所有事件；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="inventoryId"></param>
        public void DeleteIventory(int userId, int inventoryId)                                           //删除清单
        {
            var inventory=_ctx.Inventory.Include("Matter").SingleOrDefault(m => m.UserId == userId && m.InventoryId == inventoryId);
            var matters = inventory.Matter;
            _ctx.Inventory.Remove(inventory);
            _ctx.Matter.RemoveRange(matters);
            _ctx.SaveChanges();
        }
        /// <summary>
        /// 更改清单名称；
        /// 若在该user中的Inventory列表中无该inventoryId的匹配项，抛出异常；
        /// 若匹配到该Inventory对象，则更改其InventoryName;
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="inventoryId"></param>
        /// <param name="newName"></param>
        public void ChangeInventoryName(int userId, int inventoryId, string newName)                       //修改清单名称
        {
            var inventory = _ctx.Inventory.SingleOrDefault(p => p.InventoryId == inventoryId && p.UserId == userId);
            if (inventory == null)
                throw new Exception("未找到该清单！");
            inventory.InventoryName = newName;
            _ctx.SaveChanges();
        }
        /// <summary>
        /// 通过inventoryId获取该清单对象；
        /// 若没有该inventoryId匹配项，抛出异常；
        /// 若匹配到该Inventory对象，返回该对象；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        public Inventory GetInventory( int inventoryId)                                       //获得一个清单
        {
            var inventory = _ctx.Inventory.Include("Matter").SingleOrDefault(p => p.InventoryId == inventoryId);
            return inventory;
        }
        ///// <summary>
        ///// 通过userId读取所有清单；
        ///// 若该userId不存在，抛出异常；
        ///// 若存在，返回inventory列表；
        ///// </summary>
        ///// <param name="userId"></param>
        ///// <returns></returns>
        //public List<Inventory> GetInventorys(int typeId)                                                 //读取所有清单
        //{
        //    return _ctx.Type.Include("Inventory").SingleOrDefault(t => t.TypeId == typeId).Inventory.ToList();
        //}
        public List<Inventory> GetInventorysByUserId(int userId)
        {
            return _ctx.Users.Include("Inventory").Include("Matter").SingleOrDefault(u=>u.UserId==userId).Inventory.ToList();
        }

        //public int GetFirstInventoryId(ICollection<Inventory> inventorys)
        //{
        //    if (inventorys.Count == 0)
        //        return 0;
        //    return inventorys.FirstOrDefault().InventoryId;
        //}

        public void CompileInventoryModel(int typeId,int inventoryId,string inventoryName)
        {
            var inventory = _ctx.Inventory.SingleOrDefault(i => i.InventoryId == inventoryId);
            inventory.InventoryName = inventoryName;
            inventory.TypeId = typeId;
            _ctx.SaveChanges();
            
        }
        public AddInventoryModel InventoryToAddInventoryModel(UserModel user,int typeId)
        {
            AddInventoryModel aimInventory = new AddInventoryModel()
            {
                User = user,
                Type = _typeService.GetType(typeId)
            };
            return aimInventory;
        }
    }
}
